﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using Manager.Common;
using System.Linq;
using Manager.ViewModels;

namespace Manager.FormAdd_Edit
{
    public partial class frm_RoomInfo : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        PhieuDangKyPhong phieuDangKyPhong = null;
        string MaPhong = "";

        public frm_RoomInfo()
        {
            InitializeComponent();
        }

        public frm_RoomInfo(string _maPhong)
        {
            InitializeComponent();
            MaPhong = _maPhong;
        }

        private void frm_RoomInfo_Load(object sender, EventArgs e)
        {
            if (MaPhong == "")
                return;
            var phong = db.Phongs.SingleOrDefault(t => t.MaPhong == MaPhong);
            lblPhong.Text = phong.MaPhong;
            lblLoaiPhong.Text = phong.LoaiPhong1.TenLoaiPhong;
            lblSoKhach.Text = phong.LoaiPhong1.SoNguoi.ToString();
            lblGhichuPhong.Text = phong.GhiChu;
            if (phong.TinhTrang == ((int)CommonEnum.TinhTrangPhong.CoKhach_3).ToString() || phong.TinhTrang == ((int)CommonEnum.TinhTrangPhong.ToiHan_4).ToString())
            {
                phieuDangKyPhong = db.PhieuDangKyPhongs.SingleOrDefault(t => t.ChiTietPhieuDangKy.MaPhong == MaPhong && t.TinhTrang == false);
                if (phieuDangKyPhong != null)
                {
                    lblThoigianDen.Text = phieuDangKyPhong.NgayDen.Value.ToString("dd/MM/yyyy");
                    lblThoiGianDi.Text = phieuDangKyPhong.NgayDi.Value.ToString("dd/MM/yyyy");
                    var khachHang = db.KhachHangs.SingleOrDefault(t => t.MaKH == phieuDangKyPhong.MaKhachHang);
                    if (khachHang != null)
                    {
                        lblTenKhachHang.Text = khachHang.TenKH;
                    }
                    lblGia.Text = CommonFunction.AddSeparator(phieuDangKyPhong.ChiTietPhieuDangKy.GiaPhong.Value) + " " + phieuDangKyPhong.ChiTietPhieuDangKy.TienTe.MaDonVi;
                }
            }
            else if (phong.TinhTrang == ((int)CommonEnum.TinhTrangPhong.ChoKhach_5).ToString())
            {
                var ct_ctPhieuDatPhong = db.ChiTiet_ChiTietPhieuDatPhongs.SingleOrDefault(t => t.MaPhong == MaPhong);
                if (ct_ctPhieuDatPhong != null)
                {
                    lblThoigianDen.Text = ct_ctPhieuDatPhong.ChiTietPhieuDatPhong.PhieuDatPhong.NgayDen.Value.ToString("dd/MM/yyyy");
                    lblThoiGianDi.Text = ct_ctPhieuDatPhong.ChiTietPhieuDatPhong.PhieuDatPhong.NgayDi.Value.ToString("dd/MM/yyyy");
                    lblGia.Text = CommonFunction.AddSeparator(ct_ctPhieuDatPhong.ChiTietPhieuDatPhong.LoaiPhong.GiaPhong.Value);
                }
            }
            else
            {
                lblGia.Text = CommonFunction.AddSeparator(phong.LoaiPhong1.GiaPhong.Value) + " " + phong.LoaiPhong1.DonViTienTe;
            }

            LoadDichVu(phieuDangKyPhong);
        }

        private void LoadDichVu(PhieuDangKyPhong pdkp)
        {
            if (pdkp == null)
                return;
            var lstPhuThuDichVu = db.PhuThuDichVus.Where(t => t.MaPhieuDangKy == pdkp.MaPhieu).ToList();
            if (lstPhuThuDichVu.Count <= 0)
                return;
            var lstDichVuViewModel = new List<DichVuViewModel>();
            //double tongTienVND = 0;
            foreach (var phuThuDV in lstPhuThuDichVu)
            {
                lstDichVuViewModel.Add(new DichVuViewModel
                {
                    MaDV = phuThuDV.MaDichVu,
                    MaPhieuDangKy = phuThuDV.MaPhieuDangKy,
                    MaPhuThu = phuThuDV.MaPhuThu,
                    TenDichVu = phuThuDV.TenDichVu,
                    DonViTienTe = phuThuDV.MaTienTe,
                    GiaDV = phuThuDV.GiaTien,
                    strGiaDV = CommonFunction.AddSeparator(phuThuDV.GiaTien.Value),
                    NgayTao = phuThuDV.NgayTao,
                    SoLuong = phuThuDV.SoLuong.Value,
                    ThanhTien = phuThuDV.SoLuong.Value * phuThuDV.GiaTien.Value,
                    strThanhTien = CommonFunction.AddSeparator(phuThuDV.ThanhTien.Value),
                    ThanhTienVND = phuThuDV.SoLuong.Value * phuThuDV.GiaTien.Value * phuThuDV.TienTe.TyGiaThamKhao.Value,
                });                
            }
            gridDetailService.DataSource = lstDichVuViewModel;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}